package com.qianfeng.zhangwei.xutils.utils;

import android.app.Activity;
import android.view.View;

import com.qianfeng.zhangwei.xutils.annotation.ResInit;
import com.qianfeng.zhangwei.xutils.annotation.ViewInit;

import java.lang.reflect.Field;

/**
 * @说 明：
 * @项目名称：Sz1504Adv
 * @包 名： com.qianfeng.zhangwei.xutils.utils
 * @类 名： VIewUtil
 * @创 建人： zhangwei
 * @创建时间：2015-08-04 13:59
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注：
 */
public class VIewUtil {

    public static void ViewInit(Activity activity) {
        //获得当前的activity

        Class<?> cls = activity.getClass();
        Field[] fields = cls.getDeclaredFields();
        if (fields != null && fields.length > 0) {
            for (Field field : fields) {
                ViewInit viewInit = field.getAnnotation(ViewInit.class);
                if (viewInit != null) {
                    int id = viewInit.id();
                    View view = activity.findViewById(id);
                    if (view != null) {
                        field.setAccessible(true);
                        try {
                            field.set(activity, view);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    ResInit resInit = field.getAnnotation(ResInit.class);
                    Object vaule = ResUtils.loadRes(activity, resInit.id(), resInit.type());
                    if (vaule != null) {
                        field.setAccessible(true);
                        try {
                            field.set(activity, vaule);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }

    }

    public static void ViewInit(Object object, View view) {
        //获得当前的activity
        Class<?> cls = object.getClass();
        Field[] fields = cls.getDeclaredFields();
        if (fields != null && fields.length > 0) {
            for (Field field : fields) {
                ViewInit viewInit = field.getAnnotation(ViewInit.class);
                if (viewInit != null) {
                    int id = viewInit.id();
                    View vaule = view.findViewById(id);
                    if (view != null) {
                        field.setAccessible(true);
                        try {
                            field.set(object, vaule);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    ResInit resInit = field.getAnnotation(ResInit.class);
                    Object vaule = ResUtils.loadRes(view.getContext(), resInit.id(), resInit.type());
                    if (vaule != null) {
                        field.setAccessible(true);
                        try {
                            field.set(object, vaule);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }

    }


}
